package com.pwc.config;


import com.rabbitmq.client.BuiltinExchangeType;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fantuancyh
 * @data 2021/8/22
 * 基于插件实现（交换机）延迟队列，
 */
@Configuration
public class DelayQueueConfig {
    private static final String EXCHANGE_NAME = "delayed.exchange";
    private static final String QUEUE_NAME = "delayed.queue";
    private static final String ROUTING_KEY = "delayed.routingkey";

    @Bean("delayedExchange")
    CustomExchange delayedExchange(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-delayed-type","direct");
        return new CustomExchange(EXCHANGE_NAME, "x-delayed-message",
                true,false,map);
    }
    @Bean("delayedQueue")
    public Queue delayedQueue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }


    @Bean
    public Binding bindExchangeToQueue(
             @Qualifier("delayedExchange") CustomExchange delayedExchange
            ,@Qualifier("delayedQueue") Queue delayedQueue){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange)
                .with(ROUTING_KEY).noargs();
    }

}
